/* 公共部分脚本
 ** 2020-10-09
 ** by Molly
 */
// 七鱼客服
(function (w, d, n, a, j) {
  w[n] =
    w[n] ||
    function () {
      (w[n].a = w[n].a || []).push(arguments);
    };
  j = d.createElement('script');
  j.async = true;
  j.src = 'https://qiyukf.com/script/a86a4edd6f92b79c9af88d4b49e9e2d5.js?hidden=1';
  d.body.appendChild(j);
})(window, document, 'ysf');
var isSdkReady = false;
ysf('onready', function () {
  isSdkReady = true;
});

// 全局变量、公共函数
if (!MALL) var MALL = {};
var mall = {
  Common: {},
};
var common = {
  loadFlag: true,
  hideOuter: false,
  url: {
    cart_count: '/antacom/data.cart/count',
    coupon_get: '/antacom/data.Tick/getbycode',
    path_list: '/list/',
    path_tcart: '/trade/cart',
    path_torder: '/trade/order',
    path_login: '/antacom/passport/login',
    path_reg: '/antacom/passport/reg',
    path_logout: '/antacom/passport/checkout',
    path_center: '/ucenter/index',
    path_uinfos: '/ucenter/infos',
    path_orders: '/ucenter/orders',
    path_orderDetail: '/ucenter/orderDetail',
    path_aftersale: '/ucenter/aftersale',
    path_return: '/ucenter/return',
    path_pay: '/bill/home',
    path_agreement:'/cms/statement',
    agree_list:'/antacom/data.User/getAgreeList', // 获取个人信息保护政策列表

  },
  getQueryString: function (name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
      return unescape(r[2]);
    }
    return null;
  },
  search: function () {
    // 搜索
    var $srch_box = $('#search_box');
    var key = $srch_box.val();
    if (key == '') {
      key = $srch_box.attr('placeholder');
    }
    if (key == '') {
      location.href = this.url.path_list;
    } else {
      location.href = this.url.path_list + '?k=' + encodeURIComponent($srch_box.val());
    }
  },
  createScript: function (url, callback) {
    // 加载js脚本
    var oScript = document.createElement('script');
    oScript.type = 'text/javascript';
    oScript.async = true;
    oScript.src = url;

    // 判断IE8及以下浏览器
    var isIE = !-[1];
    if (isIE) {
      oScript.onreadystatechange = function () {
        if (this.readyState == 'loaded' || this.readyState == 'complete') {
          callback();
        }
      };
    } else {
      // IE9及以上浏览器，Firefox，Chrome，Opera
      oScript.onload = function () {
        callback();
      };
    }
    document.body.appendChild(oScript);
  },
  gotoTop: function () {
    $('body,html').animate({ scrollTop: 0 }, 300);
  },
  // 领取优惠券
  getCoupon: function (_this) {
    this.getCouponByCode({ code: $(_this).attr('data-code') });
  },
  getCouponByCode: function (data) {
    var that = this;
    if (that.loadFlag == false) return;
    that.loadFlag = false;
    that.showLoading();
    $.ajax({
      type: 'POST',
      url: that.url.coupon_get,
      data: { code: data.code },
      dataType: 'json',
      success: function (res) {
        that.loadFlag = true;
        that.hideLoading();
        if (res.status == 1) {
          MALL.Common.message({ text: res.msg ? res.msg : '领取成功', time: 1500 });
          data.success && typeof data.success == 'function' && data.success(res);
        } else if (res.code == 0 && res.msg && res.msg.indexOf('未登录') >= 0) {
          MALL.Common.message({ text: res.msg, time: 1500 });
          data.fail && typeof data.fail == 'function' && data.fail(res);
          setTimeout(function () {
            window.location.href = that.url.path_login;
          }, 1000);
        } else {
          MALL.Common.message({ text: res.msg ? res.msg : '领取失败', time: 1500 });
          data.fail && typeof data.fail == 'function' && data.fail(res);
        }
      },
      error: function (res) {
        that.loadFlag = true;
        that.hideLoading();
        MALL.Common.message({ text: '领取失败', time: 1500 });
        failback && typeof failback == 'function' && failback(res);
      },
    });
  },
  openkefu: function () {
    if (isSdkReady) {
      ysf('config', {
        success: function () {
          ysf('open');
        },
        error: function () {
          ysf('open');
        },
      });
    } else alert('sdk尚未加载成功，请稍后再试');
  },
  /** 获取购物车数量顺便携带登录信息 */
  getCartCount: function (data) {
    var that = this;
    $.ajax({
      type: 'POST',
      url: that.url.cart_count,
      dataType: 'json',
      success: function (res) {
        if (res.status == 1) {
          data.success && typeof data.success == 'function' && data.success(res);
        } else {
          data.fail && typeof data.fail == 'function' && data.fail(res);
        }
      },
      error: function (res) {
        data.fail && typeof data.fail == 'function' && data.fail(res);
      },
    });
  },
  checkLogin: function (res, time) {
    var _this = this;
    if (res.code == 0 && res.msg && res.msg.indexOf('未登录') >= 0) {
      setTimeout(function () {
        location.href = _this.url.path_login;
      }, time || 1000);
    }
  },
  request: function (options) {
    var useFlag = options.useFlag || false;
    var hideLoading = options.hideLoading || false;
    var checkLogin = options.checkLogin || false;
    var _this = this;
    if (useFlag && _this.loadFlag) {
      _this.loadFlag = false;
    }
    $.ajax({
      type: options.type || 'POST',
      url: options.url,
      data: options.data || {},
      async: options.async === false ? false : true,
      dataType: options.dataType || 'json',
      success: function (res) {
        if (useFlag) {
          _this.loadFlag = true;
        }
        if (hideLoading) {
          MALL.Common.hideLoading();
        }
        if (res.status && res.status > 0) {
          data.success && typeof data.success == 'function' && data.success(res);
        } else {
          data.fail && typeof data.fail == 'function' && data.fail(res);
        }
        if (checkLogin) {
          MALL.Common.checkLogin(res, options.checkLogin || 1000);
        }
      },
      error: function (res) {
        if (useFlag) {
          _this.loadFlag = true;
        }
        if (hideLoading) {
          MALL.Common.hideLoading();
        }
        data.fail && typeof data.fail == 'function' && data.fail(res);
      },
    });
  },
  /** 弹窗消息 */
  showLoading: function () {
    this.hideLoading();
    var _str =
      '<div class="dialog dialog-loading"><div class="dialog-mask"></div>\
		<div class="dialog-box"><div class="loading"></div></div></div>';
    $('body').append(_str);
  },
  hideLoading: function () {
    if ($('.dialog-loading,.dialog-message').length > 0)
      $('.dialog-loading,.dialog-message').remove();
  },
  message: function (data) {
    //data:text、time
    if (!data.text || data.text == '') {
      return false;
    }
    this.hideLoading();
    var _str =
      '<div class="dialog dialog-message"><div class="dialog-mask"></div>\
		<div class="dialog-box">' +
      data.text +
      '</div>';
    $('body').append(_str);
    this.setDialogPart($('.dialog-message'));
    setTimeout(
      function () {
        $('.dialog-message').remove();
      },
      data.time && data.time > 0 ? data.time : 2000,
    );
  },
  setDialogPart: function (dialog) {
    if (!dialog) var dialog = $('.dialog').eq(0);
    var _dw = dialog.find('.dialog-box').outerWidth(),
      _dh = dialog.find('.dialog-box').outerHeight();
    dialog.find('.dialog-box').css({
      'margin-left': '-' + _dw / 2 + 'px',
      'margin-top': '-' + _dh / 2 + 'px',
    });
  },
  alert: function (data) {
    if (!data.title || data.title == '') {
      return false;
    }
    this.hideLoading();
    var _time = new Date().getTime(),
      _str =
        '<div class="dialog dialog-alert" id="dialog-alert-' +
        _time +
        '"><div class="dialog-mask"></div><div class="dialog-box">\
			<div class="dialog-title">提示</div>\
			<div class="dialog-body"><p>' +
        data.title +
        '</p></div>\
			<div class="dialog-btns">\
			<a href="javascript:;" class="dialog-alert-yes" id="alert-yes-' +
        _time +
        '">确认</a>\
			</div></div>';
    $('body').append(_str);
    this.setDialogPart($('#dialog-alert-' + _time));
    $('#alert-yes-' + _time).click(function () {
      if (data.yes && typeof data.yes == 'function') data.yes();
      $('#dialog-alert-' + _time).remove();
    });
  },
  confirm: function (data) {
    if (!data.title || data.title == '') {
      return false;
    }
    this.hideLoading();
    var _time = new Date().getTime(),
      _str =
        '<div class="dialog dialog-confirm" id="dialog-confirm-' +
        _time +
        '"><div class="dialog-mask"></div><div class="dialog-box">\
			<div class="dialog-title">提示</div>\
			<div class="dialog-body"><p>' +
        data.title +
        '</p></div>\
			<div class="dialog-btns">\
			<a href="javascript:;" class="dialog-confirm-cancel" id="confirm-cancel-' +
        _time +
        '">取消</a>\
			<a href="javascript:;" class="dialog-confirm-yes" id="confirm-yes-' +
        _time +
        '">确定</a>\
			</div></div>';
    $('body').append(_str);
    this.setDialogPart($('#dialog-confirm-' + _time));
    $('#confirm-yes-' + _time).click(function () {
      if (data.yes && typeof data.yes == 'function') data.yes();
      $('#dialog-confirm-' + _time).remove();
    });
    $('#confirm-cancel-' + _time).click(function () {
      if (data.cancel && typeof data.cancel == 'function') data.cancel();
      $('#dialog-confirm-' + _time).remove();
    });
  },
  closedialog: function () {},
};
$.extend(MALL, mall);
$.extend(MALL.Common, common);

// 头部栏
var navList = [
  { type: 'chinastyle', url: '/list/?k=国旗', title: '冬奥国旗款', hover: false },
  {
    type: 'man',
    url: '/cms/man',
    title: '男子',
    hover: false,
    list: [
      {
        type: 'normal',
        title: '所有鞋类',
        url: '/list/e1-j1-k2',
        subs: [
          { title: '运动鞋', url: '/list/f2_f1_f5_f33-j1-k2', pic: '' },
          { title: '跑鞋', url: '/list/f2-j1-k2', pic: '' },
          { title: '篮球鞋', url: '/list/f1-j1-k2', pic: '' },
          { title: '休闲鞋', url: '/list/f3-j1-k2', pic: '' },
          { title: '板鞋', url: '/list/f4-j1-k2', pic: '' },
          { title: '户外综训鞋', url: '/list/f5_f33-j1-k2', pic: '' },
        ],
      },
      {
        type: 'normal',
        title: '所有服装',
        url: '/list/e2-j1-k2',
        subs: [
          { title: '套头卫衣', url: '/list/k2_k9-j1_j3_j4-f67', pic: '' },
          { title: '外套夹克', url: '/list/k2_k9-j1_j3_j4-f45', pic: '' },
          { title: '羽绒服/马甲', url: '/list/k2_k9-j1_j3_j4-f8', pic: '' },
          { title: '短袖T恤', url: '/list/k2_k9-j1_j3_j4-f43', pic: '' },
          { title: '长裤', url: '/list/k2_k9-j1_j3_j4-f62', pic: '' },
          { title: '运动短裤', url: '/list/k2_k9-j1_j3_j4-f53', pic: '' },
        ],
      },
      {
        type: 'normal',
        title: '所有配件',
        url: '/list/e3-k2',
        subs: [
          { title: '包类', url: '/list/k2_k9-j1_j3_j4-f29', pic: '' },
          { title: '运动袜子', url: '/list/k2_k9-j1_j3_j4-f52', pic: '' },
        ],
      },
      {
        type: 'normal',
        title: '运动项目',
        url: '',
        subs: [
          { title: '跑步', url: '/list/g6-j1-k2', pic: '' },
          { title: '篮球', url: '/list/g2-j1-k2', pic: '' },
          { title: '综训', url: '/list/g8-j1-k2', pic: '' },
          { title: '生活', url: '/list/g9-j1-k2', pic: '' },
          { title: '健身', url: '/list/g8-j1-k2', pic: '' },
        ],
      },
    ],
  },
  {
    type: 'woman',
    url: '/cms/woman',
    title: '女子',
    hover: false,
    list: [
      {
        type: 'normal',
        title: '所有鞋类',
        url: '/list/e1-j2-k2',
        subs: [
          { title: '跑鞋', url: '/list/f2-j2-k2', pic: '' },
          { title: '休闲鞋', url: '/list/f3-j2-k2', pic: '' },
          { title: '板鞋', url: '/list/f4-j2-k2', pic: '' },
          { title: '棉鞋', url: '/list/f26-j2-k2', pic: '' },
        ],
      },
      {
        type: 'normal',
        title: '所有服装',
        url: '/list/e2-j2-k2',
        subs: [
          { title: '羽绒服', url: '/list/e2-f8-j2-k2', pic: '' },
          { title: '套头卫衣', url: '/list/k2_k9-j2_j3_j4-f67', pic: '' },
          { title: '外套/夹克', url: '/list/k2_k9-j2_j3_j4-f45', pic: '' },
          { title: '长裤', url: '/list/k2_k9-j2_j3_j4-f62', pic: '' },
          { title: '短袖T恤', url: '/list/k2_k9-j2_j3_j4-f43', pic: '' },
          { title: '运动短裤', url: '/list/k2_k9-j2_j3_j4-f53', pic: '' },
        ],
      },
      {
        type: 'normal',
        title: '所有配件',
        url: '/list/e3-j2-k2',
        subs: [
          { title: '包类', url: '/list/k2_k9-j2_j3_j4-f29', pic: '' },
          { title: '运动袜类', url: '/list/k2_k9-j2_j3_j4-f52', pic: '' },
        ],
      },
    ],
  },
  {
    type: 'kids',
    url: '/cms/kids',
    title: '儿童',
    hover: false,
    list: [
      {
        type: 'new',
        subs: [
          { title: '新品上市', url: '/list/k4', pic: '', isNews: true },
          { title: '男中大童', url: '/list/j1?k=中大童', pic: '' },
          { title: '女中大童', url: '/list/j2?k=中大童', pic: '' },
          { title: '男小童', url: '/list/j1?k=小童', pic: '' },
          { title: '女小童', url: '/list/j2?k=小童', pic: '' },
          { title: '婴童', url: '/list/?k=婴童', pic: '' },
        ],
      },
      {
        type: 'normal',
        title: '所有鞋类',
        url: '/list/e1-k4',
        subs: [
          { title: '跑鞋', url: '/list/f2-k4', pic: '' },
          { title: '篮球鞋', url: '/list/f1-k4', pic: '' },
          { title: '休闲鞋', url: '/list/f3-k4', pic: '' },
          { title: '板鞋', url: '/list/f4-k4', pic: '' },
          { title: '足球鞋', url: '/list/f33-k4', pic: '' },
        ],
      },
      {
        type: 'normal',
        title: '所有服装',
        url: '/list/e2-k4',
        subs: [
          { title: '套头卫衣', url: '/list/k4-f67', pic: '' },
          { title: '羽绒服', url: '/list/e2-f8-k4', pic: '' },
          { title: '短袖T恤', url: '/list/k4-f43', pic: '' },
          { title: '运动短裤', url: '/list/k4-f125_f53', pic: '' },
          { title: '长裤', url: '/list/k4-f62', pic: '' },
        ],
      },
      {
        type: 'normal',
        title: '所有配件',
        url: '/list/e3-k4',
        subs: [
          { title: '包类', url: '/list/e3-f29-k4', pic: '' },
        ],
      },
    ],
  },
  { type: 'antakids', url: '/antakids/introduction', title: '安踏儿童', hover: false },
  { type: 'culture', url: '/culture/story', title: '品牌文化', hover: false },
  { type: 'advert', url: '/cms/advert', title: '观赏视频', hover: false },
  { type: 'contact', url: '/cms/contact', title: '联系我们', hover: false },
];
var pageType = pageType || '';
var headerVm = new Vue({
  el: '#mall_header',
  data: {
    url: MALL.Common.url,
    isLogin: false,
    username: '',
    cartNum: 0,
    keyword: '',
    placeholder: '搜索',
    navList: navList,
    pageType: pageType || '',
  },
  mounted: function () {
    var _this = this;
    this.$nextTick(function () {
      _this.refreshCartCount();
    });
  },
  methods: {
    refreshCartCount: function () {
      var _this = this;
      setTimeout(function () {
        MALL.Common.getCartCount({
          success: function (res) {
            if (res.data > 0) {
              _this.cartNum = res.data > 99 ? '99+' : res.data;
            } else {
              _this.cartNum = 0;
            }
            if (res.user && res.user != false && res.user != '') {
              _this.isLogin = true;
              _this.username = res.user;
            } else if (res.user == false) {
              _this.isLogin = false;
              _this.username = '';
            }
            try {
              if (mainVm) {
                mainVm.userInfoInCart = {
                  isLogin: _this.isLogin,
                };
              }
            } catch (error) {}
          },
          fail: function () {
            _this.cartNum = 0;
            _this.isLogin = false;
            _this.username = '';
            try {
              if (mainVm) {
                mainVm.userInfoInCart = {
                  isLogin: _this.isLogin,
                };
              }
            } catch (error) {}
          },
        });
      }, 1500);
    },
    openkefu: function () {
      MALL.Common.openkefu();
    },
    searchWords: function () {
      var key = this.keyword;
      if (key == '' && this.placeholder != '搜索') {
        key = this.placeholder;
      }
      if (key == '') {
        location.href = this.url.path_list;
      } else {
        location.href = this.url.path_list + '?k=' + encodeURIComponent(key);
      }
    },
    hoverNavItem: function (index, hover) {
      if (index < 0) {
        return false;
      }

      var navItem = this.navList[index];

      navItem.hover = hover || false;
      this.$set(this.navList, index, navItem);
    },
  },
});

// 底部栏
var footInfos = {
  busLicense: { link: '/help/60.html', name: '营业执照' },
  openPermit: { link: '/help/61.html', name: '开户许可证' },
  icp_com: { link: 'https://beian.miit.gov.cn/', name: '闽ICP备11020421号-1' },
  safe_com: {
    link: 'http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=35058202000423',
    name: '闽公网安备35058202000423号',
  },
  copyright_com: {
    en: 'Copyright(C) 2020 by www.ANTA.com',
    zh: '©安踏体育用品有限公司版权所有',
  },
  icp_cn: { link: 'https://beian.miit.gov.cn/', name: '闽ICP备2021011550号-2' },
  safe_cn: {
    link: 'http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=35020302033806',
    name: '闽公网安备35020302033806号',
  },
  copyright_cn: {
    en: 'Copyright(C) 2012-2020 by www.ANTA.cn',
    zh: '©安踏体育用品有限公司版权所有',
  },
};
var footerVm = new Vue({
  el: '#mall_footer',
  data: {
    hostType: '',
    url: MALL.Common.url,
    busLicense: footInfos.busLicense,
    openPermit: footInfos.openPermit,
    icp: false,
    safe: false,
    copyright: false,
    brands: [],
    otherLinks: [],
  },
  mounted: function () {
    if (location.host == 'www.anta.com') {
      this.hostType = 'com';
      this.icp = footInfos.icp_com;
      this.safe = footInfos.safe_com;
      this.copyright = footInfos.copyright_com;
    } else {
      this.hostType = 'cn';
      this.icp = footInfos.icp_cn;
      this.safe = footInfos.safe_cn;
      this.copyright = footInfos.copyright_cn;
    }
  },
  methods: {
    openkefu: function () {
      MALL.Common.openkefu();
    },
  },
});

// 悬浮框
var webCoverVm = new Vue({
  el: '#web-cover',
  data: {
    url: MALL.Common.url,
  },
  mounted: function () {},
  methods: {
    openkefu: function () {
      MALL.Common.openkefu();
    },
    gotoTop: function () {
      MALL.Common.gotoTop();
    },
  },
});

$(function () {
  comResizeInit();
});
window.onresize = function () {
  comResizeInit();
  pageResizeInit();
};
function comResizeInit() {
  /* 公共部分 resize时调用函数*/
  //头部logo
  var logo_ml = Math.ceil(($('.J_header').width() - 1280) / 2);
  $('.J_head_logo').css('margin-left', logo_ml > 0 ? logo_ml + 'px' : 0);
}
function pageResizeInit() {
  /* 页面窗口resize时调用函数，各页面内重新写并覆盖 */
}
